// RoundTripFlightDetail.vue - 往返航班详情组件
<template>
  <div class="roundtrip-flight-detail">
    <!-- 往返组合概览 -->
    <el-card shadow="never" class="detail-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">
            <el-icon><Switch /></el-icon>
            往返机票组合
          </span>
          <el-tag type="primary" size="small">组合套票</el-tag>
        </div>
      </template>

      <!-- 组合总览 -->
      <div class="combination-overview">
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="overview-item">
              <div class="label">行程ID</div>
              <div class="value">{{ flight.itineraryId }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="overview-item">
              <div class="label">成人总价</div>
              <div class="value highlight">{{ flight.currency }} {{ flight.adultTotalPrice }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="overview-item">
              <div class="label">航班类型</div>
              <div class="value">
                <el-tag :type="flight.isAllDirectFlight ? 'success' : 'warning'" size="small">
                  {{ getTransferDescription() }}
                </el-tag>
              </div>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" style="margin-top: 16px;">
          <el-col :span="24">
            <div class="route-overview">
              <div class="route-info">
                {{ getRouteDescription() }}
              </div>
              <div class="route-detail">
                {{ getAirportDescription() }}
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 去程详情 -->
    <el-card shadow="never" class="detail-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">
            去程航班
          </span>
          <div class="flight-tags">
            <el-tag type="success" size="small">去程</el-tag>
            <el-tag v-if="getOutboundTransferCount() === 0" type="success" size="small">
              直飞
            </el-tag>
            <el-tag v-else type="warning" size="small">
              {{ getOutboundTransferCount() }}次中转
            </el-tag>
          </div>
        </div>
      </template>

      <div v-if="flight.outboundFlight" class="flight-detail-section">
        <OneWayFlightDetailContent :flight="flight.outboundFlight" />
      </div>
      <div v-else class="no-data">
        <el-empty description="去程航班信息不完整"></el-empty>
      </div>
    </el-card>

    <!-- 返程详情 -->
    <el-card shadow="never" class="detail-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">
            返程航班
          </span>
          <div class="flight-tags">
            <el-tag type="primary" size="small">返程</el-tag>
            <el-tag v-if="getReturnTransferCount() === 0" type="success" size="small">
              直飞
            </el-tag>
            <el-tag v-else type="warning" size="small">
              {{ getReturnTransferCount() }}次中转
            </el-tag>
          </div>
        </div>
      </template>

      <div v-if="flight.returnFlight" class="flight-detail-section">
        <OneWayFlightDetailContent :flight="flight.returnFlight" />
      </div>
      <div v-else class="no-data">
        <el-empty description="返程航班信息不完整"></el-empty>
      </div>
    </el-card>

    <!-- 往返价格详情 -->
    <el-card shadow="never" class="detail-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">
            <el-icon><Money /></el-icon>
            往返价格明细
          </span>
        </div>
      </template>

      <div class="roundtrip-price-details">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="price-breakdown">
              <h4>成人价格</h4>
              <div class="price-line">
                <span>往返总价：</span>
                <span class="price highlight">{{ flight.currency }} {{ flight.adultTotalPrice }}</span>
              </div>
              <div class="price-components">
                <div class="component-line">
                  <span>去程：</span>
                  <span class="price">{{ flight.currency }} {{ getOutboundAdultPrice() }}</span>
                </div>
                <div class="component-line">
                  <span>返程：</span>
                  <span class="price">{{ flight.currency }} {{ getReturnAdultPrice() }}</span>
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="12" v-if="hasChildPrice()">
            <div class="price-breakdown">
              <h4>儿童价格</h4>
              <div class="price-line">
                <span>往返总价：</span>
                <span class="price highlight">{{ flight.currency }} {{ flight.childTotalPrice }}</span>
              </div>
              <div class="price-components">
                <div class="component-line">
                  <span>去程：</span>
                  <span class="price">{{ flight.currency }} {{ getOutboundChildPrice() }}</span>
                </div>
                <div class="component-line">
                  <span>返程：</span>
                  <span class="price">{{ flight.currency }} {{ getReturnChildPrice() }}</span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" v-if="hasInfantPrice()" style="margin-top: 20px;">
          <el-col :span="12">
            <div class="price-breakdown">
              <h4>婴儿价格</h4>
              <div class="price-line">
                <span>往返总价：</span>
                <span class="price highlight">{{ flight.currency }} {{ flight.infantTotalPrice }}</span>
              </div>
              <div class="price-components">
                <div class="component-line">
                  <span>去程：</span>
                  <span class="price">{{ flight.currency }} {{ getOutboundInfantPrice() }}</span>
                </div>
                <div class="component-line">
                  <span>返程：</span>
                  <span class="price">{{ flight.currency }} {{ getReturnInfantPrice() }}</span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>

        <!-- 往返组合产品信息 -->
        <div class="combination-product-info">
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="info-item">
                <span class="label">舱位等级：</span>
                <el-tag :type="getCabinType()" size="small">
                  {{ getCabinLabel() }}
                </el-tag>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="info-item">
                <span class="label">余票数量：</span>
                <span class="value">{{ flight.ticketCount || '充足' }}</span>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="info-item">
                <span class="label">痛苦指数：</span>
                <span class="value">{{ flight.miseryIndex || 'N/A' }}</span>
              </div>
            </el-col>
          </el-row>
        </div>
      </div>
    </el-card>

    <!-- 服务信息 -->
    <el-card shadow="never" class="detail-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">
            <el-icon><Service /></el-icon>
            服务信息
          </span>
        </div>
      </template>

      <div class="service-info">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="info-section">
              <h4>行李政策</h4>
              <p class="baggage-info">{{ flight.baggageInfo || '请咨询航空公司具体行李政策' }}</p>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-section">
              <h4>购买须知</h4>
              <p class="policy-info">{{ getPurchaseNotes() }}</p>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" style="margin-top: 20px;">
          <el-col :span="8">
            <div class="info-item">
              <span class="label">最后更新：</span>
              <span class="value">{{ formatDateTime(flight.lastUpdated) }}</span>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 温馨提示 -->
    <el-card shadow="never" class="detail-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">
            <el-icon><InfoFilled /></el-icon>
            温馨提示
          </span>
        </div>
      </template>

      <div class="tips-content">
        <el-alert
            v-if="!flight.isAllDirectFlight"
            title="中转注意事项"
            type="warning"
            :closable="false"
            show-icon>
          <template #default>
            <ul class="tips-list">
              <li>此往返行程包含{{ flight.totalTransferCount }}次中转，请预留充足的中转时间</li>
              <li v-if="getOutboundTransferCount() > 0">去程包含{{ getOutboundTransferCount() }}次中转，建议至少提前2小时到达中转机场</li>
              <li v-if="getReturnTransferCount() > 0">返程包含{{ getReturnTransferCount() }}次中转，请注意核实后续航班信息</li>
              <li>行李托运政策可能因中转而有所不同，请提前确认</li>
              <li>如遇航班延误，请及时联系航空公司安排后续行程</li>
            </ul>
          </template>
        </el-alert>

        <el-alert
            v-else
            title="往返直飞"
            type="success"
            :closable="false"
            show-icon>
          <template #default>
            <p>此往返行程为全程直飞，无需中转，旅行更便捷省心。</p>
          </template>
        </el-alert>

        <div style="margin-top: 16px;">
          <el-alert
              title="往返机票说明"
              type="info"
              :closable="false"
              show-icon>
            <template #default>
              <ul class="tips-list">
                <li>往返机票为组合票价，去程和返程必须同时使用</li>
                <li>请确保护照有效期至少6个月以上（国际航班）</li>
                <li>请提前确认签证要求和入境政策</li>
                <li>建议购买旅行保险以保障行程安全</li>
              </ul>
            </template>
          </el-alert>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import {
  Money,
  Service,
  InfoFilled,
  Switch,
} from '@element-plus/icons-vue'
import OneWayFlightDetailContent from './OneWayFlightDetailContent.vue'
import cityNames from "@/utils/cityNames.js"
import airportNames from "@/utils/airportNames.js"
import airlineNames from "@/utils/airlineNames.js"

// Props
const props = defineProps({
  flight: {
    type: Object,
    required: true
  }
})

// 方法
const getTransferDescription = () => {
  if (props.flight.isAllDirectFlight) {
    return '往返直飞'
  }
  return `共${props.flight.totalTransferCount}次中转`
}

const getRouteDescription = () => {
  const outbound = props.flight.outboundFlight
  const returnFlight = props.flight.returnFlight

  if (!outbound || !returnFlight) return ''

  const outboundRoute = `${getCityName(outbound.departureCityCode)}-${getCityName(outbound.arrivalCityCode)}`
  const returnRoute = `${getCityName(returnFlight.departureCityCode)}-${getCityName(returnFlight.arrivalCityCode)}`

  return `${outboundRoute} ↔ ${returnRoute}`
}

const getAirportDescription = () => {
  const outbound = props.flight.outboundFlight
  const returnFlight = props.flight.returnFlight

  if (!outbound || !returnFlight) return ''

  const outboundAirports = `${getAirportName(outbound.departureAirportCode)}-${getAirportName(outbound.arrivalAirportCode)}`
  const returnAirports = `${getAirportName(returnFlight.departureAirportCode)}-${getAirportName(returnFlight.arrivalAirportCode)}`

  return `${outboundAirports} / ${returnAirports}`
}

const getOutboundTransferCount = () => {
  return props.flight.outboundFlight?.transferCount || 0
}

const getReturnTransferCount = () => {
  return props.flight.returnFlight?.transferCount || 0
}

const getOutboundAdultPrice = () => {
  return props.flight.outboundFlight?.adultTotalPrice || 0
}

const getReturnAdultPrice = () => {
  return props.flight.returnFlight?.adultTotalPrice || 0
}

const getOutboundChildPrice = () => {
  return props.flight.outboundFlight?.childTotalPrice || 0
}

const getReturnChildPrice = () => {
  return props.flight.returnFlight?.childTotalPrice || 0
}

const getOutboundInfantPrice = () => {
  return props.flight.outboundFlight?.infantTotalPrice || 0
}

const getReturnInfantPrice = () => {
  return props.flight.returnFlight?.infantTotalPrice || 0
}

const hasChildPrice = () => {
  return props.flight.childTotalPrice && props.flight.childTotalPrice > 0
}

const hasInfantPrice = () => {
  return props.flight.infantTotalPrice && props.flight.infantTotalPrice > 0
}

const getCabinType = () => {
  const typeMap = {
    'Y': 'success',
    'Y_S': 'success',
    'C': 'warning',
    'C_S': 'warning',
    'F': 'danger',
    'F_S': 'danger'
  }
  return typeMap[props.flight.cabinClass] || 'info'
}

const getCabinLabel = () => {
  const labelMap = {
    'Y': '经济舱',
    'Y_S': '经济舱',
    'C': '商务舱',
    'C_S': '商务舱',
    'F': '头等舱',
    'F_S': '头等舱'
  }
  return labelMap[props.flight.cabinClass] || props.flight.cabinClass
}

const getPurchaseNotes = () => {
  const notes = props.flight.purchaseNotes
  if (Array.isArray(notes)) {
    return notes.join('; ')
  }
  return notes || '请咨询航空公司'
}

const formatDateTime = (dateTimeStr) => {
  if (!dateTimeStr) return '--'
  try {
    return dateTimeStr.substring(0, 16).replace('T', ' ')
  } catch (e) {
    return dateTimeStr
  }
}

const getAirportName = (airportCode) => {
  return airportNames[airportCode] || airportCode
}

const getCityName = (cityCode) => {
  return cityNames[cityCode] || cityCode
}
</script>

<style scoped>
.roundtrip-flight-detail {
  max-width: 1200px;
  margin: 0 auto;
}

.detail-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: bold;
}

.flight-tags {
  display: flex;
  gap: 8px;
}

/* 组合概览样式 */
.combination-overview {
  padding: 16px;
  background-color: #f0f9ff;
  border-radius: 8px;
  margin-bottom: 20px;
}

.overview-item {
  text-align: center;
}

.overview-item .label {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.overview-item .value {
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.overview-item .value.highlight {
  color: #f56c6c;
  font-size: 18px;
}

.route-overview {
  text-align: center;
  padding: 16px;
  background-color: white;
  border-radius: 8px;
  border: 1px solid #e4e7ed;
}

.route-info {
  font-size: 18px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 8px;
}

.route-detail {
  font-size: 13px;
  color: #666;
}

/* 航班详情区域样式 */
.flight-detail-section {
  padding: 0;
}

.no-data {
  padding: 40px;
  text-align: center;
}

/* 往返价格详情样式 */
.roundtrip-price-details {
  padding: 0;
}

.price-breakdown {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 16px;
  background-color: #fafafa;
}

.price-breakdown h4 {
  margin: 0 0 12px 0;
  color: #333;
  font-size: 16px;
}

.price-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  font-size: 16px;
  font-weight: bold;
  padding-bottom: 12px;
  border-bottom: 1px solid #e4e7ed;
}

.price-line .price.highlight {
  color: #f56c6c;
  font-size: 18px;
}

.price-components {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.component-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: #666;
}

.component-line .price {
  font-family: 'Courier New', monospace;
  color: #333;
}

.combination-product-info {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e4e7ed;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.info-item .label {
  font-size: 14px;
  color: #666;
  min-width: 80px;
}

.info-item .value {
  font-size: 14px;
  color: #333;
}

/* 服务信息样式 */
.service-info {
  padding: 0;
}

.info-section {
  margin-bottom: 16px;
}

.info-section h4 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 14px;
}

.baggage-info, .policy-info {
  font-size: 13px;
  color: #666;
  line-height: 1.5;
  margin: 0;
  padding: 8px 12px;
  background-color: #f8f9fa;
  border-radius: 4px;
  border-left: 3px solid #409eff;
}

/* 温馨提示样式 */
.tips-content {
  padding: 0;
}

.tips-list {
  margin: 0;
  padding-left: 20px;
  line-height: 1.6;
}

.tips-list li {
  margin-bottom: 8px;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .roundtrip-flight-detail {
    padding: 0 10px;
  }

  .combination-overview {
    padding: 12px;
  }

  .overview-item .value {
    font-size: 14px;
  }

  .overview-item .value.highlight {
    font-size: 16px;
  }

  .route-info {
    font-size: 16px;
  }

  .price-breakdown {
    padding: 12px;
  }

  .price-line {
    font-size: 14px;
  }

  .price-line .price.highlight {
    font-size: 16px;
  }
}

/* 动画效果 */
.detail-card {
  transition: all 0.3s ease;
}

.detail-card:hover {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.price.highlight {
  animation: price-pulse 2s infinite;
}

@keyframes price-pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.7;
  }
}
</style>